package com.boot.modules.profile.controller;

import com.boot.common.anno.AnnoLog;
import com.boot.common.anno.RepeatSubmit;
import com.boot.common.result.Result;
import com.boot.modules.profile.service.ProfileService;
import com.boot.system.service.UserService;
import com.boot.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/profile")
public class ProfileController {

    private final ProfileService profileService;

    private final UserService userService;

    @AnnoLog(value = "获取登录用户信息")
    @GetMapping("/me")
    public Result<?> getLoginUserInfo() {
        return Result.success(profileService.getLoginUserInfo());
    }

    @AnnoLog(value = "更改当前密码")
    @RepeatSubmit
    @PostMapping("/changePassword")
    public Result<?> changePassword(@RequestBody Map<String, Object> dataParam) {
        return userService.changePassword((String) dataParam.get("oldPassword"), (String) dataParam.get("newPassword"));
    }

    @AnnoLog(value = "上传头像")
    @RepeatSubmit
    @PostMapping("/uploadAvatar")
    public Result<?> uploadAvatar(@RequestParam("file") MultipartFile file) {
        return profileService.uploadAvatar(file);
    }

    @AnnoLog(value = "更新用户信息")
    @RepeatSubmit
    @PostMapping("/updateUser")
    public Result<?> updateUser(@RequestBody Map<String, Object> dataParam) {
        dataParam.put("id", SecurityUtil.getUid());
        return userService.updateUser(dataParam);
    }

}
